package com.cqw.commontest.reflect;

import java.lang.reflect.Method;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-08-31 23:00
 * @Description: 反射测试
 */
public class ReflectTest {

    public static void main(String[] args) throws Exception {
        //获取class文件的方法
        Class<Book> bookClass = Book.class;

        Book book = new Book();
        Class<? extends Book> aClass = book.getClass();

        Class<?> aClass1 = Class.forName("com.cqw.commontest.reflect.Book");

        //获取类对象的方法和参数
        Method[] methods = bookClass.getMethods();






        Method getName = bookClass.getMethod("getName");
        Book book1 = bookClass.newInstance();
        book1.setName("曹家三少");
        Object invoke = getName.invoke(book1);
        //System.out.println(invoke);


        Method getBook = bookClass.getMethod("getBook", String.class);
        Book book2 = bookClass.newInstance();
        Object invoke1 = getBook.invoke(book2, "JAVA");
        System.out.println(invoke1);


    }


}
